package nsqp

import (
	"bgs/conf"
	// "bgs/grpc/gen/proxy2/inventory"
	// "bgs/grpc/imp/enums"
	// "encoding/json"
	// "fmt"
	"github.com/nsqio/go-nsq"
	"go.uber.org/zap"
	// "time"
)

const (
	topicOfOrderNotifyToInventory = "order_notify"
	topicOfOrderUpdateToInventory = "order_update"
)

// type enumActionOfOrderNotifyToInventory string

// const (
	// actionOfOrderNotifyToInventoryReceived receive incude picked and received
	// actionOfOrderNotifyToInventoryReceived enumActionOfOrderNotifyToInventory = "received"

	// actionOfOrderUpdateToInventoryRefundApply 售后申请
	// actionOfOrderUpdateToInventoryRefundApply enumActionOfOrderUpdateToInventory = "refund_apply"

	// actionOfOrderUpdateToInventoryRefundCancel 售后取消
	// actionOfOrderUpdateToInventoryRefundCancel enumActionOfOrderUpdateToInventory = "refund_cancel"
// )

// orderUpdateToInventory topicOfOrderUpdateToInventory's body
// type orderUpdateToInventory struct {
// 	OrderNumber string               `json:"order_number"`
// 	Action      string               `json:"action"`
// 	Timestamp   int64                `json:"timestamp"`
// 	Source      inventory.SourceEnum `json:"source"`
// }

// func makeProducerTopic(topic string) string {
// 	return fmt.Sprintf("%s-%s", enums.APPIDBGS, topic)
// }

// func makeTopic(appID enums.EnumAppID) func(string) string {
// 	return func(topic string) string {
// 		return fmt.Sprintf("%s-%s", appID, topic)
// 	}
// }

// NsqProducerProxy publish nsq message producer
type NsqProducerProxy struct {
	slog     *zap.SugaredLogger
	producer *nsq.Producer
}

// NewNsqProducerProxy initialization
func NewNsqProducerProxy(
	nsqConf conf.NsqSection,
	slog *zap.SugaredLogger,
) (proxy *NsqProducerProxy, err error) {
	proxy = &NsqProducerProxy{
		slog: slog.With("module", "NsqProducerProxy"),
	}
	config := nsq.NewConfig()

	producer, err := nsq.NewProducer(nsqConf.NsqdAddress, config)
	if err != nil {
		return
	}
	proxy.producer = producer
	producer.SetLogger(
		nsqLogger,
		nsq.LogLevelError,
	)

	// Gracefully stop the consumer.
	// consumer.Stop()

	return
}

// // PublishOrderPickedParam order picked
// type PublishOrderPickedParam struct {
// 	OrderSerialNumber string
// 	Picked            time.Time
// }

// // PublishOrderPicked 发送订单确认自提消息
// func (s *NsqProducerProxy) PublishOrderPicked(p PublishOrderPickedParam) (err error) {
// 	msg := orderUpdateToInventory{
// 		OrderNumber: p.OrderSerialNumber,
// 		Action:      string(actionOfOrderNotifyToInventoryReceived),
// 		Timestamp:   p.Picked.Unix(),
// 		Source:      inventory.SourceEnum_XT_ORDER,
// 	}
// 	body, err := json.Marshal(msg)
// 	if err != nil {
// 		return
// 	}
// 	err = s.producer.Publish(makeTopic(enums.APPIDINVENTORY)(topicOfOrderNotifyToInventory), body)
// 	return
// }

// // PublishOrderReceivedParam order received
// type PublishOrderReceivedParam struct {
// 	OrderSerialNumber string
// 	Received          time.Time
// }

// // PublishOrderReceived 发送订单确认收货消息
// func (s *NsqProducerProxy) PublishOrderReceived(p PublishOrderReceivedParam) (err error) {
// 	msg := orderUpdateToInventory{
// 		OrderNumber: p.OrderSerialNumber,
// 		Action:      string(actionOfOrderNotifyToInventoryReceived),
// 		Timestamp:   p.Received.Unix(),
// 		Source:      inventory.SourceEnum_XT_ORDER,
// 	}
// 	body, err := json.Marshal(msg)
// 	if err != nil {
// 		return
// 	}
// 	err = s.producer.Publish(makeTopic(enums.APPIDINVENTORY)(topicOfOrderNotifyToInventory), body)
// 	return
// }
